﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonInterfaces.Models;
using System.Windows.Controls;
using System.Windows.Input;
using GUI.Custom_Classes;
using System.Windows;

namespace StandardScriptItems.SimpleControls
{
    public class ElementStateComboBox : EditableComboBox
    {
        public delegate void ElementSelectedDelegate(String element);
        public event ElementSelectedDelegate ElementSelectionChanged;

        private IElementState state;

        public ElementStateComboBox() : base("--Which Element?--", new List<String>())
        {
        }

        public ElementStateComboBox(IElementState state)
            : this(state, "--Which Element?--")
        {}

        public ElementStateComboBox(IElementState state, String src)
            : base(src, state.Elements)
        {
            Name = "ElementSelector";
            this.state = state;
            ElementSelectionChanged += delegate(String str) { };
            SelectionChanged += delegate(object sender, SelectionChangedEventArgs args)
            {
                ElementSelectionChanged(ToString());
            };
        }

        public override ComboBoxItem Add(object obj)
        {
            ComboBoxItem item = base.Add(obj);
            
            item.MouseMove += delegate(object sender, MouseEventArgs args)
            {
                try
                {
                    if (item.Content is String)
                    {
                        Rect rect = state.GetAE(item.Content.ToString()).Current.BoundingRectangle;
                        Hilighter_Box.Singleton.Target(rect);
                    }
                }
                catch (Exception e) { e = e;/*Couldn't get the element. Alas, we can not highlight it.*/ }
            };

            item.MouseLeave += delegate(object sender, MouseEventArgs args)
            {
                Hilighter_Box.Singleton.Hide();
            };

            return item;
        }

        public void Filter(LEET.CommonInterfaces.Filter<String> test)
        {
            Clear();
            foreach (String elem in state.Elements)
            {
                if (test(elem))
                {
                    Add(elem);
                }
            }
        } 
    }
}
